var express = require('express');
var router = express.Router();
var result = require('../utils/result')
var fs = require('fs');
var multer  = require('multer')
var createFolder = function(folder){
    try{
        fs.accessSync(folder);
    }catch(e){
        fs.mkdirSync(folder);
    }
};

var uploadFolder = '../public/upload/';

createFolder(uploadFolder);

// 通过 filename 属性定制
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, uploadFolder);    // 保存的路径，备注：需要自己创建
    },
    filename: function (req, file, cb) {
        /*将保存文件名设置为 字段名 + 时间戳，比如 logo-1478521468943
        cb(null, file.fieldname + '-' + Date.now());*/
        let fileFormat = (file.originalname).split(".");
        let filename = `${file.fieldname}.${new Date().getTime()}.${file.originalname}`
        cb(null, filename);
    }
});

// 通过 storage 选项来对 上传行为 进行定制化
var upload = multer({ storage: storage })

// 单图上传
router.post('/api/upload', upload.single('avatar'), function(req, res, next){
    var file = req.file;
    console.log(req.file)
    res.json(result(0,'success',{imgUrl:`/upload/${req.file.filename}`}));

});


module.exports = router;
